package com.xpgk.mvc.bloseWeight;

import java.util.Date;
import java.util.List;

import org.bouncycastle.asn1.dvcs.Data;

import com.jfinal.plugin.activerecord.Record;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.codeSet.Code;


public class BloseWeightController extends  BaseController{
	
	private BloseWeightService  bloseWeightService;
	
	public  void  index()
	{	
		
		String cusIds=getPara("cusIds");
		if(cusIds!=null)
		{
			Customer  cus=Customer.dao.findById(cusIds);
			setAttr("customer", cus);
		}
		render("BloseWeight.html");
	}
	
	/**
	 * 跳转至添加
	 */

	public void toAddTarget(){
		Record record = DelWeiTargetSum.dao.findCustomerLastOneTarget(ids);
		if(null != record){
			setAttr("_startTime", new Date());
		}
		
		String sql = "select weight from sb_customer_service where customerID = ? ORDER BY startTime DESC";
		CustomerService customerService = CustomerService.dao.findFirst(sql,ids);
		if(customerService!=null){
			setAttr("_nowWeight", customerService.getFloat(CustomerService.column_weight));
		}else{
			String sql2 = "SELECT nowWeight FROM sb_customer_detail where customerIds = ? ";
			CustomerDetail customerDetail = CustomerDetail.dao.findFirst(sql2,ids);
			
			if(customerDetail!=null){
				setAttr("_nowWeight", customerDetail.getFloat(CustomerDetail.column_nowWeight));
			}
			
		}
		setAttr("customer", Customer.dao.findById(ids));
		render("addTarget.html");
	}
	
	/**
	 * 添加
	 */
	public void addTarget(){
		DelWeiTargetSum target = getModel(DelWeiTargetSum.class);
		List<DelWeiTarget> targets = getModels(DelWeiTarget.class);
		RenderBean renderBean = bloseWeightService.addTarget(target, targets, getCUserIds());
		renderJson(renderBean);
	}
	
	/**
	 * 跳转至修改
	 */
	public void toUpTarget(){
		setAttr("targetSum", DelWeiTargetSum.dao.findById(ids));
		render("upTarget.html");
	}
	
	/**
	 * 获取目标
	 */
	public void getTargets(){
		List<DelWeiTarget> dwt = DelWeiTarget.dao.findTargetBySum(ids);
		renderJson(dwt);
	}
	
	/**
	 * 修改
	 */
	public void upTarget(){
		List<DelWeiTarget> targets = getModels(DelWeiTarget.class);
		RenderBean renderBean = bloseWeightService.upTarget(targets, getCUserIds());
		renderJson(renderBean);
	}
	
	/**
	 * 删除
	 */
	public void deleteTarget(){
		DelWeiTargetSum delWeiTargetSum=   DelWeiTargetSum.dao.findById(ids);
		
		//List<DelWeiTarget> targets = getModels(DelWeiTarget.class);
		RenderBean renderBean = bloseWeightService.deleteTarget(delWeiTargetSum);
		renderJson(renderBean);
	}
	/**
	 * 目标汇总列表
	 */
	public void targetList(){
		String json = paging(splitPage, DelWeiTargetSum.sqlId_splitPageSelect, DelWeiTargetSum.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	/**获取减重目标模式下排期
	 * 
	 */
	public  void getSchedule()
	{	
		String moshiIds=getPara("moshiIds");
		List<Code> cList=Code.cacheGetChild(moshiIds);
		renderJson(cList);
	}
	
	/**
	 * 制定目标（弹窗）
	 * */
	public  void makeTarget()
	{	
		String  cusIds=getPara("customerIds");
		Customer  cus=Customer.dao.findById(cusIds);
		setAttr("customer", cus);	
		
		String sql = "select weight from sb_customer_service where customerID = ? ORDER BY startTime DESC";
		CustomerService customerService = CustomerService.dao.findFirst(sql,cusIds);
		if(customerService!=null){
			setAttr("_nowWeight", customerService.getFloat(CustomerService.column_weight));
		}else{
			String sql2 = "SELECT nowWeight FROM sb_customer_detail where customerIds = ? ";
			CustomerDetail customerDetail = CustomerDetail.dao.findFirst(sql2,cusIds);
			setAttr("_nowWeight", customerDetail.getFloat(CustomerDetail.column_nowWeight));
		}
		
	
		render("/customer/cusBloseWeight.html");
	}
	
}
